package com.lizardmind.haofudoctor.ui.diagnosis.sickMan.advisory.kaifang.addChuFang

import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.View
import android.widget.GridLayout
import android.widget.LinearLayout
import android.widget.TextView
import com.google.gson.Gson
import com.linxiao.framework.BaseApplication
import com.linxiao.framework.activity.BaseActivity
import com.linxiao.framework.util.*
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.databinding.ActivityChooseMedicineBinding
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.*
import io.reactivex.functions.Action
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_choose_medicine.*
import kotlinx.android.synthetic.main.include_title_text.*
import net.sourceforge.pinyin4j.PinyinHelper
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.linearLayout
import org.jetbrains.anko.padding
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.toast

//药材选择activity
class ChooseMedicineActivity : BaseActivity() {

    var vis_code : String by DelegatesExt.preference(BaseApplication.getAppContext(),"visCode","0")//版本号
    var medicineData : String by DelegatesExt.preference(BaseApplication.getAppContext(),"medicineData","[]")

    val listA_Z= arrayListOf("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","w","x","y","z")
    val list= arrayListOf<ChooseMedicineBean>()
    val listMedicineNames= arrayListOf<String>()

    val adapter by lazy { ChooseMedicineAdapter(this,list) }

    val binding by lazy { getDataBinding<ActivityChooseMedicineBinding>(R.layout.activity_choose_medicine) }
    override fun onCreatRootView(): View? {
        return binding.root
    }

    override fun onInitView(savedInstanceState: Bundle?) {

        for ( i in 0..5){
            addTags("发挥得舒服"+i)
        }

        for (i in 0 until listA_Z.size){

//            if (listA_Z[i] == "v"){ break }

            val params=LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,0,1f)
            val textView=TextView(this)
            textView.text=listA_Z[i]
            textView.textSize=10f
            textView.setPadding(6,0,12,0)
            textView.layoutParams=params
            lLayoutChooseMedicineTag.addView(textView)
            textView.onClick {
                toast(listA_Z[i])
                rvChooseMedicine.scrollToPosition(i)
            }
            list.add(ChooseMedicineBean(listA_Z[i], arrayListOf()))
        }

        titleText.text="药材选择"
        imgLeft.setImageResource(R.mipmap.back)
        imgLeft.onClick { finish() }
        imgRight.setImageResource(R.mipmap.kf_sure)
        imgRight.onClick {
            EventBus.getDefault().post(AddMedicineEvent(listMedicineNames))
            finish()
        }

        rvChooseMedicine.apply {
            layoutManager= LinearLayoutManager(this@ChooseMedicineActivity)
            adapter=this@ChooseMedicineActivity.adapter
        }

        adapter.setOnItemPointClick(object : ChooseMedicineAdapter.OnItemPointClick{
            override fun onItemPointClick(text: String) {
                addTags(text)
                listMedicineNames.add(text)
            }
        })

        //药材名tags删除监听
        delTagMedicalNames.setTagClickListener(object : DeleteTagsLayout.TagClickListener{
            override fun tagClick(position: Int) {
                listMedicineNames.removeAt(position)
                delTagMedicalNames.removeViewAt(position)
            }
        })

        getMedicine()

    }

    //添加药材
    private fun addTags(string: String){
        val textView=TextView(this)
        textView.setTextColor(ContextCompat.getColor(this,R.color.gray3))
        val draw=ContextCompat.getDrawable(this,R.mipmap.delete)
        draw.setBounds(0,0,draw.minimumWidth,draw.minimumHeight)
        textView.setCompoundDrawables(null,null,draw,null)
        textView.textSize = 15f
        textView.text=string
        delTagMedicalNames.addView(textView)
    }

    //获取药材
    private fun getMedicine(){
        postApi<Request<ChooseMedicineBeanApi>>(hashMapOf("action" to "medical",
                "token" to DoctorInfo.token,
                "version" to vis_code),
//                "version" to "0"),
                Consumer {
                    it.dispose(notData = {
                        list.clear()
                        list.addAll(Gson().fromJson<List<ChooseMedicineBean>>(medicineData))
                    },data = {
                        vis_code = it.version

                        it.data.forEach {
                            val a = PinyinHelper.toHanyuPinyinStringArray(it.title[0])[0]
                            val asciiNum=a[0].toInt()-'a'.toInt()
                            //拼音中没有v开头的字，用ascii码计算时v后面的拼音需要-1
                            list[if (asciiNum<('v'.toInt()-'a'.toInt())) asciiNum else asciiNum-1].listMedicine.add(it)
                        }
//                        list.removeAt(21)
                        medicineData = Gson().toJson(list)
                    })
                }, Action {
            adapter.notifyDataSetChanged()
        },context = this)
    }

}
